package com.dylan.历史.java8.lambda表达式.练习题;

import com.dylan.历史.util.SleepUtils;

import java.util.*;

/**
 * Created by Dylan on 2018/7/19.
 */
public class 第8题 {

    // lambda表达是获取局部变量值
    public static void main(String[] args) {
        String[] names = new String[]{"Peter", "Paul", "Mary"};
        List<Runnable> runners = new ArrayList<>();
        for (String name : names) {
            runners.add(() -> System.out.println(name));
        }
        runners.stream().map(Thread::new).forEach(Thread::start);

        SleepUtils.seconds(1);
        runners.clear();
        for (int i = 0; i < names.length; i++) {
            String name = names[i];
            runners.add(() -> System.out.println(name));
        }
        runners.stream().map(Thread::new).forEach(Thread::start);

        SleepUtils.seconds(1);
        Arrays.asList(new String[]{"Peter", "Paul", "Mary"}).stream().map(name -> (Runnable) () -> System.out.println(name)).map(Thread::new).forEach(Thread::start);
    }
}
